package com.common.core.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @auther zhangwj
 * @date 2021/11/29 下午7:59
 */
public class CodeUtil {

    private static final String NUMBER = "0123456789";

    private static final String VARCHAR = "0123456789abcdefghijklmnopqrstuvwxyz";

    private static final Random RANDOM = new SecureRandom();

    public static String getNonceNumber(int length) {
        // 如果需要4位，那 new char[4] 即可，其他位数同理可得
        char[] nonceChars = new char[length];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = NUMBER.charAt(RANDOM.nextInt(NUMBER.length()));
        }
        return new String(nonceChars);
    }

    public static String getNonceStr(int length) {
        // 如果需要4位，那 new char[4] 即可，其他位数同理可得
        char[] nonceChars = new char[length];
        for (int index = 0; index < length; ++index) {
            nonceChars[index] = VARCHAR.charAt(RANDOM.nextInt(VARCHAR.length()));
        }
        return new String(nonceChars);
    }
}
